package com.ymatou.iapi.productlist.testcase;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productlist.service.GetCatalogListByCatalogIdListCall;
import com.ymatou.iapi.productlist.service.GetProductListByProductIdListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.mongo.ProductBasicInfoWapper;
import com.ymttest.database.mongo.ProductStockInfoWapper;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;

public class Ts_GetCatalogListByCatalogIdList {
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据规格ID list,查询商品描述信息");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new GetCatalogListByCatalogIdListCall().getOpurl());
	}
	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetCatalogListByCatalogIdList() {
		Logger.start(true, "根据规格ID List,查询商品描述信息");
		try{
			Set<String> keyPathsSet =  new HashSet<String>();
			
			String productId1 = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			String catalogsId1 = new YmtCatalogsIWapper().selectCatalogsByProductId(productId1).get(0).getsCatalogId();
			
			String productId2 = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			String catalogsId2 = new YmtCatalogsIWapper().selectCatalogsByProductId(productId2).get(0).getsCatalogId();
			
			JSONObject retBean = new GetCatalogListByCatalogIdListCall().callAndGetReturnData(MapUtil.hashMap("CatalogIdList", new JSONArray(Arrays.asList(catalogsId1,catalogsId2))));
			Logger.verifyEquals(200,retBean.getInt("Code"),  "响应报文-返回码");
			
			JSONArray ProductList = retBean.getJSONObject("Data").getJSONArray("ProductList");
			Logger.verifyEquals(2,ProductList.length(),  "响应报文-productList");
			
			for (int i=0;i<ProductList.length();i++){
				JSONObject productBean = ProductList.getJSONObject(i);
				String productId = productBean.getString("ProductId");
				String catalogsId = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).get(0).getsCatalogId();
				
				//mongo check 
				if (YmtMongoDriverFactroy.isOldMongoCheck()){
					Map<String, Object> productBasicInfoMap =  new ProductBasicInfoWapper().getByProductId(productId);
					keyPathsSet.clear();
					keyPathsSet.add("ProductId");
					keyPathsSet.add("SellerName");
					keyPathsSet.add("SellerId");
					keyPathsSet.add("BrandId");
					AssertService.assertResultEqual(keyPathsSet,productBasicInfoMap,productBean);
					
					
					JSONObject catalogStockBean = productBean.getJSONObject("Price").getJSONObject("Catalog");
					Map<String, Object> productStockInfoMap =  new ProductStockInfoWapper().getByCatalogId(catalogsId);
					keyPathsSet.clear();
					keyPathsSet.add("CatalogId");
					
					AssertService.assertResultEqual(keyPathsSet,productStockInfoMap,catalogStockBean);
				}
				
				
				
			}
			
			
			
		
		}catch(Exception e){
			Logger.fail(e);
		}
		
		
		
		
		
	}
}
